//
//  XLPlayerItem.m
//  FFMpegKitDemo
//
//  Created by ilongge on 2023/8/2.
//

#import "XLPlayerItem.h"
#import "XLPlayerError.h"
#import <AVFoundation/AVFoundation.h>

@implementation XLPlayerItem

+ (instancetype)playerItemWithURL:(NSURL *)URL error:(NSError *__autoreleasing *)error
{
    XLPlayerItem *item = [[XLPlayerItem alloc] initWithURL:URL error:error];
    return item;
}

+ (instancetype)playerItemWithAsset:(AVURLAsset *)asset error:(NSError *__autoreleasing *)error
{
    XLPlayerItem *item = [[XLPlayerItem alloc] initWithAsset:asset error:error];
    return item;
}

- (instancetype)initWithURL:(NSURL *)URL error:(NSError *__autoreleasing *)error
{
    if (URL == nil) {
        *error = [XLPlayerError errorWithCode:XLPlayerErrorCodeUrlNil];
        return nil;
    }
    else if (URL.path == nil) {
        *error = [XLPlayerError errorWithCode:XLPlayerErrorCodeUrlInvalid];
        return nil;
    }
    self = [super init];
    if (self) {
        _url = URL;
        _asset = [AVURLAsset assetWithURL:URL];
    }
    return self;
}

- (instancetype)initWithAsset:(AVURLAsset *)asset error:(NSError *__autoreleasing *)error
{
    if (asset == nil) {
        *error = [XLPlayerError errorWithCode:XLPlayerErrorCodeAssetNil];
        return nil;
    }
    else if (asset.URL == nil) {
        *error = [XLPlayerError errorWithCode:XLPlayerErrorCodeUrlNil];
        return nil;
    }
    else if (asset.URL.path == nil) {
        *error = [XLPlayerError errorWithCode:XLPlayerErrorCodeUrlInvalid];
        return nil;
    }
    self = [super init];
    if (self) {
        _url = asset.URL;
        _asset = asset;
    }
    return self;
}
@end
